package com.jeeplus.modules.sys.SSO.utiks;


import okhttp3.*;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;

import java.io.IOException;

public class OkHttpUtil {


    public static String getToken(String LoginID, String LoginPWD,String url) {
        String str="";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
            RequestBody body = RequestBody.create(mediaType, "LoginID="+LoginID+"&LoginPWD="+LoginPWD);
            Request request = new Request.Builder()
                    .url(url)
                    .method("POST", body)
                    .addHeader("Content-Type", "application/x-www-form-urlencoded")
                    .build();
        Response response = client.newCall(request).execute();
         str = response.body().string();
        } catch (ClientProtocolException e) {
            System.err.println("Http协议出现问题");
            e.printStackTrace();
        } catch (ParseException e) {
            System.err.println("解析错误");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IO异常");
            e.printStackTrace();
        }
        return str;
    }

    public static String getSSOUser(String token, String condition,String url) {
        String str="";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, condition);
            Request request = new Request.Builder()
                    .url(url)
                    .method("POST", body)
                    .addHeader("token", token)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            str = response.body().string();
        } catch (ClientProtocolException e) {
            System.err.println("Http协议出现问题");
            e.printStackTrace();
        } catch (ParseException e) {
            System.err.println("解析错误");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IO异常");
            e.printStackTrace();
        }
        return str;
    }


}
